package gat.api.rest.background.backgroundUser;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bitsun.core.common.persistence.Pager;
import com.bitsun.core.common.web.ResultDTO;

import gat.api.background.backgroundUser.BackgroundUserApi;
import gat.api.config.annotation.CheckPermissions;
import gat.api.config.annotation.Log;
import gat.application.backgroundUser.BackgroundUserService;
import gat.dto.req.backgroundUser.BackgroundUserReqDto;
import gat.dto.req.backgroundUser.BackgroundUserReqParams;
import gat.dto.res.backgroundUser.BackgroundUserResDto;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @Author: tmac
 * @Email: tmac
 */
@Slf4j
@SuppressWarnings("unchecked")
@RestController
@RequestMapping("/backgroundUser")
public class BackgroundUserApiController implements BackgroundUserApi {


    @Autowired
    private BackgroundUserService backgroundUserService;

    @Override
    @Log(value = "管理后台用户保存")
    @CheckPermissions(value = "backgroundUser:save")
    @PostMapping(consumes = {"application/json"}, produces = {"application/json"})
    public ResultDTO<BackgroundUserResDto> addBackgroundUser(
        @ApiParam(value = "要保存的对象", required = true) @Valid @RequestBody BackgroundUserReqDto reqDto) {
        BackgroundUserResDto resDto = backgroundUserService.save(reqDto);
        return ResultDTO.ok(resDto);
    }

    @Override
    @Log(value = "管理后台用户列表查询")
    @CheckPermissions(value = "backgroundUser:list")
    @GetMapping(produces = {"application/json"})
    public ResultDTO<Pager<BackgroundUserResDto>> getBackgroundUserList(BackgroundUserReqParams params) {
        Pager<BackgroundUserResDto> pager = backgroundUserService.getBackgroundUserList(params);
        return ResultDTO.ok(pager);
    }

    @Override
    @Log(value = "管理后台用户手机号登录")
    @PostMapping(value = "/loginByPhone")
    public ResultDTO<BackgroundUserResDto> loginByPhone(@RequestBody BackgroundUserReqParams params) {
        BackgroundUserResDto resDto = backgroundUserService.loginByPhone(params);
        return ResultDTO.ok(resDto);
    }

    @Override
    @Log(value = "管理后台用户密码登录")
    @PostMapping(value = "/login",consumes = {"application/json"}, produces = {"application/json"})
    public ResultDTO<BackgroundUserResDto> login(@RequestBody BackgroundUserReqParams params) {
        BackgroundUserResDto resDto = backgroundUserService.login(params);
        return ResultDTO.ok(resDto);
    }

    @Override
    @Log(value = "管理后台用户修改手机号")
    @CheckPermissions(value = "backgroundUser:updatePhone")
    @PostMapping(value = "/updatePassword",consumes = {"application/json"}, produces = {"application/json"})
    public ResultDTO<Boolean> updatePassword(@RequestBody BackgroundUserReqParams params) {
        Boolean boo = backgroundUserService.updatePassword(params);
        return ResultDTO.ok(boo);
    }

    @Override
    @Log(value = "管理后台用户重置密码")
    @CheckPermissions(value = "backgroundUser:resetPassword")
    @PutMapping(value = "/resetPassword",produces = {"application/json"})
    public ResultDTO<Boolean> resetPassword(@RequestBody BackgroundUserReqParams params) {
        Boolean boo = backgroundUserService.resetPassword(params);
        return ResultDTO.ok(boo);
    }

    @Override
    @Log(value = "管理后台用户删除")
    @CheckPermissions(value = "backgroundUser:delete")
    @DeleteMapping(value = "/{ids}", produces = {"application/json"})
    public ResultDTO<Void> deleteBackgroundUser(
        @ApiParam(value = "要删除的对象主键，多个对象主键可以用英文逗号隔开", required = true) @PathVariable("ids") String ids) {
        boolean bl = backgroundUserService.deleteByIds(ids);
        return ResultDTO.ok();
    }

    @Override
    @GetMapping(value = "/{id}", produces = {"application/json"})
    public ResultDTO<BackgroundUserResDto> getOneBackgroundUser(@ApiParam(value = "要查询的对象主键", required = true) @PathVariable("id") Long id) {
        BackgroundUserResDto resDto = backgroundUserService.selectOne(id);
        return ResultDTO.ok(resDto);
    }


    @Override
    @Log(value = "管理后台用户编辑")
    @CheckPermissions(value = "backgroundUser:edit")
    @PatchMapping(value = "/{id}", produces = {"application/json"})
    public ResultDTO<BackgroundUserResDto> patchUpdateBackgroundUser(@ApiParam(value = "对象ID", required = true) @PathVariable("id") Long id,
        @ApiParam(value = "要修改的对象，对象属性有值的才更新,即null值不更新", required = true) @Valid @RequestBody BackgroundUserReqDto reqDto) {
        backgroundUserService.updateProps(id, reqDto);
        BackgroundUserResDto resDto = backgroundUserService.selectOne(id);
        return ResultDTO.ok(resDto);
    }

    @Override
    @PostMapping("/signOut")
    public ResultDTO<Void> signOut() {
        backgroundUserService.signOut();
        return ResultDTO.ok();
    }

    @Override
    @PostMapping(value = "/sendSMS/{phone}", produces = {"application/json"})
    public ResultDTO<Void> sendSMS(@PathVariable("phone") String phone) {
        Boolean aBoolean = backgroundUserService.sendSMS(phone);
        return ResultDTO.ok(aBoolean);
    }
}
